<?php
$this->breadcrumbs = array(
	'费用报销单' => array(
		'listExpense'
	),
	'生成报销单',
);

$this->userActions = array(
	array(
		'label' => '费用报销单',
		'url' => array(
			'listExpense'
		)
	),
);
?>

<h1>生成报销单</h1>

<div class="form">
<?php $form = $this
	->beginWidget('CActiveForm',
		array(
			'id' => 'material-expense-form',
			'enableAjaxValidation' => false,
		));
?>
	<?php echo $form->errorSummary($model); ?>

	<div class="row">
		<?php echo $form->labelEx($model, 'expenseDate'); ?>
		<?php $this
			->widget('zii.widgets.jui.CJuiDatePicker',
				array(
					'model' => $model,
					'attribute' => 'expenseDate',
				));
		?>
		<?php echo $form->error($model, 'expenseDate'); ?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model, 'note'); ?>
		<?php echo $form
			->textField($model, 'note', array(
				'size' => 60,
				'maxlength' => 255
			));
		?>
		<?php echo $form->error($model, 'note'); ?>
	</div>
<fieldset>
	<legend>包含票据：</legend>
	<div >
	<?php
	$this
		->widget('zii.widgets.grid.CGridView',
			array(
				'id' => 'material-receipt-grid',
				'dataProvider' => new CArrayDataProvider($model->getGenerateReceipts(),
					array(
						'pagination' => false
					)),
				'template' => "{items}",
				'enablePagination' => false,
				'columns' => array(
					array(
						'class' => 'CCheckBoxColumn',
						'name' => 'id',
						'selectableRows' => 10,
						'checked' => 'true',
						'checkBoxHtmlOptions' => array(
							'name' => CHtml::activeName($model, 'receiptIds[]'),
						)
					),
					array(
						'header' => '',
						'type' => 'raw',
						'value' => '$row+1'
					),
					array(
						'name' => 'project.name',
						'header' => '项目',
					),
					array(
						'name' => 'receiptDate',
						'header' => '日期',
					),
					array(
						'header' => '商家',
						'type' => 'raw',
						'value' => '$data->merchantId?$data->merchant->name:" "'
					),
					array(
						'name' => 'handler',
						'header' => '经手人',
					),
					array(
						'name' => 'totalPrice',
						'type' => 'currency',
						'header' => '总价',
					),
					array(
						'name' => 'note',
						'header' => '备注',
					),
				),
			));
	?>
	</div>
</fieldset>
<?php if (!empty($model->receiptIds)) { ?>
	<div class="row buttons">
	<?php echo CHtml::submitButton('应用', array());
	?>
		<?php echo CHtml::submitButton('生成',
				array(
					'confirm' => '确定要生成这张报销单？生成后将不可编辑',
					'name' => 'generate',
				));
		?>
	<?php echo CHtml::submitButton('预览',
			array(
				'name' => 'preview',
				'id' => 'preview-button',
			));
		$previewUrl = CJSON::encode($this->createUrl('viewExpense', array(
				'print' => 1
			)));
		$script = <<<EOF
$('#preview-button').click(function(){
	var form, action;
	form=$('#material-expense-form');
	action=form.attr('action');
	form.attr('action',$previewUrl);
	form.attr('target','_blank');
	form.submit();
	form.attr('action',action);
	form.attr('target','');
	return false;
});
EOF
		;
		Yii::app()->clientScript->registerScript('material-expense-preview', $script);
	?>
	</div>
<?php } ?>
<div class="dynamic-add-item">
<div>查找票据</div>
<div class="span-8">
<?php if ($project = $model->project) { ?>
<label>项目:</label>
<?php echo $project->name; ?>
<?php echo CHtml::hiddenField('', $project->id,
		array(
			'id' => 'dynamic-material-receipt-project',
		));
?>
<?php } else { ?>
<?php echo CHtml::label('选择项目', 'dynamic-material-receipt-project'); ?>
<?php $this->widget('widgets.project.JProjectSelector',
				array(
					'name' => '',
					'value' => '',
					'htmlOptions'=>array('id' => 'dynamic-material-receipt-project'),
				));
?>
<?php } ?>
</div>
<div class="span-8">
<?php echo CHtml::label('选择票据日期:', 'dynamic-material-receipt-date'); ?>
<?php $this
	->widget('zii.widgets.jui.CJuiDatePicker',
		array(
			'id' => 'dynamic-material-receipt-date-min',
			'name' => '',
			'value' => '',
			'htmlOptions' => array(
				'size' => '12',
			),
		));
?> - <?php $this
		 ->widget('zii.widgets.jui.CJuiDatePicker',
			 array(
				 'id' => 'dynamic-material-receipt-date-max',
				 'htmlOptions' => array(
					 'size' => '12',
				 ),
				 'name' => '',
				 'value' => date('Y-m-d'),
			 ));
	 ?>
</div>
<div class="span-2 last">
<?php echo CHtml::button('搜索',
	array(
		'id' => 'dynamic-material-receipt-search',
		'class' => 'button'
	));
?>
</div>
<div class="clear"></div>
<div id="dynamic-material-receipt-list"></div>
<?php echo CHtml::submitButton('添加选中的票据',
	array(
		'confirm' => '确定要添加选中的票据么？',
		'name' => 'add',
	));
?>
<?php
$dynamicLoadItemsUrl = CJSON::encode($this->createUrl('materialReceipt/dynamicLoadItems'));
$js = <<<EOF
$('#dynamic-material-receipt-search').click(function(){
	var projectId=$('#dynamic-material-receipt-project').val();
	var dateMin=$('#dynamic-material-receipt-date-min').val();
	var dateMax=$('#dynamic-material-receipt-date-max').val();
	if(projectId&&dateMax){
		$('#dynamic-material-receipt-list').load($dynamicLoadItemsUrl+'?projectId='+projectId
		+'&dateMin='+dateMin+'&dateMax='+dateMax);
	}
});
EOF
;
Yii::app()->clientScript->registerScript('dynamic-material-receipt-search', $js);
?>
</div><!-- dynamic-add-item -->
<?php $this->endWidget(); ?>

</div><!-- form -->
